#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import urllib2
import string
from Route import Route,Schedule


vacation_info_re_pre = re.compile(r'''
        <line>(.*?)<\s*/\s*line>
        ''', re.X|re.S)

vacation_info_re = re.compile(r'''
        ^(.*?)<\s*schedule>
        ''', re.X|re.S)

xml_cdata_node_re = re.compile(r'''
        <\s*(\w+)\s*>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\s*/\s*\1\s*>
        ''', re.X|re.S)

xml_schedule_re = re.compile(r'''
        <schedule>(.*?)<\s*/\s*schedule>
        ''', re.X|re.S)

xml_price_re = re.compile(r'''
        <price>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\/price>
        ''', re.X|re.S)

xml_person_re = re.compile(r'''
        <personnum>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\/personnum>
        ''', re.X|re.S)
xml_dates_re = re.compile(r'''
        <departdate>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\/departdate>
        ''', re.X|re.S)

def do_crawl():
    try:
        server_url = 'http://www.utourworld.com/InternationLine/SinaLine.xml'
        # TODO
        #html, _ = curl_handle.open(server_url)
        html = urllib2.urlopen(server_url).read()
        vacation_info_list = vacation_info_re_pre.findall(html)
        #print vacation_info_list
        for vacation_info_pre in vacation_info_list:
            #print "-----------------------------------------------------"
            vacation_info = re.search(vacation_info_re, vacation_info_pre).group(1)
            vacation_info_dict = dict(xml_cdata_node_re.findall(vacation_info))
            if not vacation_info_dict:
                print >>sys.stderr, 'cannot match xml_node_re'
                continue
            
            v = vacation_info_dict
            route = Route()
            route.site = '20'
            route.supplier_url = "http://www.utourworld.com/"
            route.supplier = "众信国旅"
            #print 'title: ' + v.get('title', '')
            route.title = v.get('title', '')
            #print 'url: ' + v.get('url', '')
            route.rout_url = v.get('url', '')
            #print 'duration: ' + v.get('duration', '')
            route.days = v.get('duration', '')
            #print 'startcity: ' + v.get('startcity', '')
            route.outcity = v.get('startcity', '')
            #print 'avenue: ' + v.get('avenue', '')
            route.sights = v.get('avenue', '')
            #print 'callcenter: ' + v.get('callcenter', '')
            route.telphone = v.get('callcenter', '')
            #print 'characteristic: ' + v.get('characteristic', '') + v.get('tips', '')
            route.detail = v.get('characteristic', '') + v.get('tips', '')
            #print 'departcountry: ' + v.get('departcountry', '')
            route.dest = v.get('departcountry', '')
            #print 'guideservice: ' + v.get('guideservice', '')
            route.guide = v.get('guideservice', '')
            
            if v.get('type', '').strip() == '1':
                route.type = "旅行团"
            else:
                route.type = "自由行" 
                
            route.go_t = route.back_t = '飞机'
            
            #print 'presonnum: ' + getPerson(vacation_info_pre)
            #route.person = getPerson(vacation_info_pre)
            #print 'plane: ' + getPlane(vacation_info_pre)
            route.plane = getPlane(vacation_info_pre)
            #print 'price: ' + getPrice(vacation_info_pre)
            route.price = getPrice(vacation_info_pre)
            #print 'dates: ' + getDates(vacation_info_pre)
            route.dates = getDates(vacation_info_pre)
            route.schedules = getSchedules(vacation_info_pre)
            
            print >>sys.stderr, 'updating', route.supplier, route.title
            route.updRoute()
            route.updRoute_Schedules()
    except:
        print traceback.format_exc(sys.exc_info())

def getSchedules(vacation_info_pre):
     
     plane_info_list = xml_schedule_re.findall(vacation_info_pre)
     schedules = range(0,len(plane_info_list))
     for plane_info in plane_info_list:
        plane_info_dict = dict(xml_cdata_node_re.findall(plane_info))
        if not plane_info_dict:
            print >>sys.stderr, 'cannot match xml_node_re'
            continue
        v = plane_info_dict
        schedule = Schedule()
        schedule.s_num=v.get('serialnumber','')
        schedule.s_place=v.get('place','')
        schedule.s_brief=v.get('brief','')
        schedule.s_brf="1" if v.get('breakfast','').strip()=='有' else "0"
        print schedule.s_brf
        schedule.s_lunch="1" if v.get('s_lunch','').strip()=='有' else "0"
        #print schedule.s_lunch
        schedule.s_dinner="1" if v.get('dinner','').strip()=='有' else "0"
        #print schedule.s_dinner
        car_re = re.compile(r'''>v<''', re.X|re.S)
        plane_re = re.compile(r'''>Q<''', re.X|re.S)
        
        if plane_re.search(v.get('DayTitle','')):
            schedule.s_traffic="飞机"
            strs = v.get('DayTitle','').split("航班：")
            if len(strs) > 1:
                schedule.s_plcode = strs[1]
        if car_re.search(v.get('DayTitle','')):
            schedule.s_traffic="汽车"
        #print schedule.s_traffic
        #print schedule.s_plcode
        
        schedule.s_hotel=v.get('accommodation','')
        schedules[string.atoi(v.get('serialnumber',''))-1]=schedule
        #print schedule.s_hotel
     return schedules

def getPrice(vacation_info_pre):
    temp = xml_price_re.search(vacation_info_pre)
    if temp:
        return temp.group(1)
    return ""

def getPerson(vacation_info_pre):
    temp = xml_person_re.search(vacation_info_pre)
    if temp:
        return temp.group(1)
    return ""

def getPlane(vacation_info_pre):
     plane_info_list = xml_schedule_re.findall(vacation_info_pre)
     plane_str = ''
     for plane_info in plane_info_list:
        plane_info_dict = dict(xml_cdata_node_re.findall(plane_info))
        if not plane_info_dict:
            print >>sys.stderr, 'cannot match xml_node_re'
            continue
        v = plane_info_dict
        plane_str = plane_str + "#第" + v.get('serialnumber','') + "天# "
        plane_str = plane_str + v.get('brief','')
     return plane_str
 
def getDates(vacation_info_pre):
     dates_info_list = xml_dates_re.findall(vacation_info_pre)
     dates_str = ""
     for dates_info in dates_info_list:
         dates_str = dates_str + dates_info + ";"
     return dates_str
     
if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()